package com.seanzx.dp.processor;

import com.seanzx.dp.common.Persist;
import com.seanzx.common.Result;
import com.seanzx.common.ResultCode;

import java.io.File;
import java.io.IOException;
import java.util.List;

public class DatabaseProcessor {
    public Result<?> create(String databaseName) throws IOException {
        File database = Persist.getDatabase(databaseName);
        if (database.exists()) {
            return Result.of(ResultCode.DATABASE_ALREADY_EXISTS);
        }
        if (!database.mkdir()) {
            return Result.of(ResultCode.CREATE_DATABASE_FAILED);
        }
        return Result.ok();
    }

    public Result<?> drop(String databaseName) throws IOException {
        File database = Persist.getDatabase(databaseName);
        if (!database.exists()) {
            return Result.of(ResultCode.DATABASE_NOT_EXISTS);
        }
        if (!database.delete()) {
            return Result.of(ResultCode.DROP_DATABASE_FAILED);
        }
        return Result.ok();
    }

    public Result<Boolean> exists(String databaseName) throws IOException {
        File database = Persist.getDatabase(databaseName);
        return Result.ok(database.exists());
    }

    public Result<List<String>> showDatabase() {
        return Result.ok(Persist.showDatabases());
    }
}
